package com.hwl.eduservice.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hwl.commonutils.result.RespBean;
import com.hwl.eduservice.pojo.Course;
import com.hwl.eduservice.pojo.vo.CourseInfoForm;
import com.hwl.eduservice.pojo.vo.CourseQueryVo;
import com.hwl.eduservice.pojo.vo.CourseVo;
import com.hwl.eduservice.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-09-21
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/admin/eduCourse")
public class CourseController {

    @Resource
    private CourseService courseService;

    @ApiOperation("添加课程")
    @PostMapping("addCourse")
    public RespBean addCourse(@RequestBody CourseInfoForm courseInfoForm) {
        //返回添加的课程id
        return RespBean.ok().message("添加课程信息成功！").data(courseService.addCourseInfo(courseInfoForm));
    }

    @ApiOperation("根据id查询课程")
    @GetMapping("getCourseInfo/{courseId}")
    public RespBean getCourseInfo(@PathVariable String courseId) {
        return RespBean.ok().data(courseService.getCourseInfo(courseId));
    }

    @ApiOperation("更新课程信息")
    @PutMapping("updateCourseInfo")
    public RespBean updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        if (courseService.updateCourseInfo(courseInfoForm)) {
            return RespBean.ok().message("更新课程信息成功！");
        }
        return RespBean.error().message("更新课程信息失败！");
    }

    @ApiOperation("课程分页列表")
    @GetMapping("list/{page}/{limit}")
    public RespBean getAllCourse(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                                 @ApiParam(value = "每页大小", required = true) @PathVariable Long limit,
                                 CourseQueryVo courseQueryVo) {
        IPage<CourseVo> iPage = courseService.selectPage(page, limit, courseQueryVo);
        return RespBean.ok().data(iPage);
    }

    @ApiOperation("删除课程信息")
    @DeleteMapping("remove/{id}")
    public RespBean removeById(@PathVariable String id) {
        //删除课程封面
        courseService.removeCoverById(id);
        //删除课程信息
        if (courseService.removeCourseById(id)) {
            return RespBean.ok().message("删除课程信息成功！");
        }
        return RespBean.error().message("删除课程信息失败！");
    }

    @ApiOperation("根据id获取课程的发布信息")
    @GetMapping("getCoursePublish/{id}")
    public RespBean getCoursePublishVoById(@PathVariable String id) {
        return RespBean.ok().data(courseService.getCoursePublishVoById(id));
    }

    @ApiOperation("课程的最终发布")
    @PutMapping("releaseCourse/{id}")
    public RespBean releaseCourse(@PathVariable String id) {
        Course course = new Course().setStatus(Course.COURSE_NORMAL);
        course.setId(id);
        if (courseService.updateById(course)) {
            return RespBean.ok().message("课程发布成功！");
        }
        return RespBean.error().message("课程发布失败！");
    }

}

